package com.sxt.sys.service.impl;

import java.io.Serializable;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.common.Constast;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.TreeNode;
import com.sxt.sys.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.mapper.DeptMapper;
import com.sxt.sys.service.DeptService;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2019-09-23
 */
@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
	
	@Override
	public Dept getById(Serializable id) {
		return super.getById(id);
	}
	
	@Override
	public boolean updateById(Dept entity) {
		return super.updateById(entity);
	}
	
	@Override
	public boolean removeById(Serializable id) {
		return super.removeById(id);
	}
	
	@Override
	public boolean save(Dept entity) {
		return super.save(entity);
	}

	/**
	 * 加载部门管理左边的部门树的json
	 */
	public List<TreeNode> loadDeptManagerLeftTreeJson(DeptVo deptVo) {
		List<Dept> list = this.list();
		List<TreeNode> treeNodes=new ArrayList<>();
		for (Dept dept : list) {
			Boolean spread=dept.getOpen()==1?true:false;
			treeNodes.add(new TreeNode(dept.getId(), dept.getPid(), dept.getTitle(), spread));
		}

		return treeNodes;
	}

	/**
	 * 查询
	 */
	public IPage<Dept> loadAllDept(DeptVo deptVo) {
		IPage<Dept> page=new Page<>(deptVo.getPage(), deptVo.getLimit());
		QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()), "title", deptVo.getTitle());
		queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()), "address", deptVo.getAddress());
		queryWrapper.like(StringUtils.isNotBlank(deptVo.getRemark()), "remark", deptVo.getRemark());
		queryWrapper.eq(deptVo.getId()!=null, "id", deptVo.getId()).or().eq(deptVo.getId()!=null,"pid", deptVo.getId());
		queryWrapper.orderByAsc("ordernum");
		this.page(page, queryWrapper);
		return page;
	}

	/**
	 * 加载最大的排序码
	 * @param
	 * @return
	 */
	public List<Dept> loadDeptMaxOrderNum(){
		QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("ordernum");
		IPage<Dept> page=new Page<>(1, 1);
		List<Dept> list = this.page(page, queryWrapper).getRecords();
		return list;
	}


	/**
	 * 添加
	 * @param deptVo
	 * @return
	 */
	public Boolean addDept(DeptVo deptVo) {
		try {
			deptVo.setCreatetime(new Date());
			this.save(deptVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}


	/**
	 * 添加
	 * @param deptVo
	 * @return
	 */
	public Boolean updateDept(DeptVo deptVo) {
		try {
			this.updateById(deptVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}



	/**
	 * 查询当前的ID的部门有没有子部门
	 */
	public List<Dept> checkDeptHasChildrenNode(DeptVo deptVo){
		QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("pid", deptVo.getId());
		List<Dept> list = this.list(queryWrapper);
		return list;
	}

	/**
	 * 删除
	 * @param deptVo
	 * @return
	 */
	public Boolean deleteDept(DeptVo deptVo) {
		try {
			this.removeById(deptVo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 加载门诊部门的树列表
	 */
	public List<Dept> loadDeptpForSelectById() {
		QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("pid", Constast.MENZHEN_ID);
		List<Dept> list = this.list(queryWrapper);
		return list;
	}

}
